﻿using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Filter
{
    /// <summary>
    /// 动作过滤器
    /// </summary>
    public class ActionLogFilter: ActionFilterAttribute
    {
        private readonly ILogger<ActionLogFilter> logger;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="logger"></param>
        public ActionLogFilter(ILogger<ActionLogFilter> logger)
        {
            this.logger = logger;
        }
        /// <summary>
        /// 执行前接口记录
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var httpcontext = context.HttpContext;
            var inparam = context.ActionArguments;
            string InParam = JsonConvert.SerializeObject(inparam.Values);
            var request = httpcontext.Request;
            var ip= httpcontext.Connection.RemoteIpAddress;
            var nowuser = httpcontext.User.FindFirstValue("UserName");
            if (ip != null)
                ip.ToString();
            var url = request.Path;
            var method = request.Method;
            string lastPart = "";
            if(url.Value!=null)
            {
                int lastIndex = url.Value.LastIndexOf('/'); // 查找最后一个'/'的位置  
                {
                    lastPart = url.Value.Substring(lastIndex + 1); // 从'/'后面的位置开始提取子字符串  
                }
            }        
            if (lastPart != "DowLoadExcel" && lastPart!= "UploadExcel" && (method == "POST" || method == "DELETE"))
            {
                if (ip != null)
                    logger.LogInformation($"IP：{ip}，操作人：{nowuser}，接口地址:{url}，参数:{InParam}");
                else
                    logger.LogInformation($"IP：UNKNOWN，操作人：{nowuser}，接口地址:{url}，参数:{InParam}");
            }
            base.OnActionExecuting(context);
        }

        /// <summary>
        /// 执行后出参
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            var httpcontext = context.HttpContext;
            var request = httpcontext.Request;
            var ip = httpcontext.Connection.RemoteIpAddress;
            var nowuser = httpcontext.User.FindFirstValue("UserName");
            if (ip != null)
                ip.ToString();
            var url = request.Path;
            var method = request.Method;
            string lastPart = "";
            if (url.Value != null)
            {
                int lastIndex = url.Value.LastIndexOf('/'); // 查找最后一个'/'的位置  
                {
                    lastPart = url.Value.Substring(lastIndex + 1); // 从'/'后面的位置开始提取子字符串  
                }
            }
            if (lastPart != "DowLoadExcel" && lastPart!= "UploadExcel" && lastPart != "UploadFile" && (method == "POST" || method == "DELETE"))
            {
                if (context.Result is ObjectResult objectResult)
                {
                    var returnValue = JsonConvert.SerializeObject(objectResult.Value);
                    if (ip != null)
                        logger.LogInformation($"IP：{ip}，操作人：{nowuser}，接口地址:{url}，出参:{returnValue}");
                    else
                        logger.LogInformation($"IP：UNKNOWN，操作人：{nowuser}，接口地址:{url}，出参:{returnValue}");
                }
            }
            base.OnActionExecuted(context);
        }

    }
}
